#pragma semicolon 1 // Force strict semicolon mode.

#include <sourcemod>
#include <sdktools>
#include <tf2_stocks>
#include <psycore>
#include <tfx>
#include <sdkhooks>

#define PLUGIN_NAME              "tLoot - KellerIdeas'"
#define PLUGIN_AUTHOR            "Thrawn"
#define PLUGIN_VERSION           "1.0.0.0"
#define PLUGIN_CONTACT           "http://aaa.einfachonline.net/"

#define SOUND_FITNESSTOM		"vo/scout_apexofjump01.wav"
#define SOUND_PUMPKIN			"items/pumpkin_drop.wav"
#define SOUND_CANTSTANDSTILL2	"misc/rubberglove_snap.wav"
#define SOUND_CANTSTANDSTILL1   "misc/rubberglove_stretch.wav"

new g_iEffectNum[8] = {-1,...};
new Handle:g_clientEffectTimer[MAXPLAYERS + 1][2];

new bool:g_bHasFitnessTom[MAXPLAYERS + 1] = {false, ...};
new bool:g_bHasCantStandStill[MAXPLAYERS + 1] = {false, ...};

public Plugin:myinfo = {
    name        = PLUGIN_NAME,
    author      = PLUGIN_AUTHOR,
    description = PLUGIN_NAME,
    version     = PLUGIN_VERSION,
    url         = PLUGIN_CONTACT
};

public OnMapStart()
{
	//PrecacheModel(MODEL_CONE, true);
	PrecacheSound(SOUND_FITNESSTOM, true);
	PrecacheSound(SOUND_CANTSTANDSTILL1, true);
	PrecacheSound(SOUND_CANTSTANDSTILL2, true);
	PrecacheSound(SOUND_PUMPKIN, true);
}

public OnAllPluginsLoaded() {
	if(LibraryExists("psycore")) {
		g_iEffectNum[0] = psyRTD_RegisterEffect(psyRTDEffectType_Bad,"Fitness Tom","Jump up, jump up and get down!",EffectFitnessTom);
		g_iEffectNum[1] = psyRTD_RegisterEffect(psyRTDEffectType_Bad,"Can't stand still","It's itching everywhere!",EffectCantStandStill);
		g_iEffectNum[2] = psyRTD_RegisterEffect(psyRTDEffectType_Bad,"Halloween","We'll have some pumpkin soup!",EffectPumpkin);
	}		
}

public DisableEffect(client,effectId) {
	for(new i = 0; i < sizeof(g_iEffectNum); i++) {
		if(effectId == g_iEffectNum[i]) {
			if(g_clientEffectTimer[client][0] != INVALID_HANDLE)
				TriggerTimer(g_clientEffectTimer[client][0]);
			if(g_clientEffectTimer[client][1] != INVALID_HANDLE)
				TriggerTimer(g_clientEffectTimer[client][1]);
		}
	}
}

stock ApplyForce(client, Float:vec[3], Float:amount = 50.0) {
	ScaleVector(vec, amount);
	
	new Float:playerspeed[3];
	GetEntPropVector(client, Prop_Data, "m_vecVelocity", playerspeed);
	AddVectors(vec, playerspeed, vec);
	
	TeleportEntity(client, NULL_VECTOR, NULL_VECTOR, vec);	
}


public psyRTDAction:EffectFitnessTom(client) {
	g_bHasFitnessTom[client] = true;
	
	g_clientEffectTimer[client][0] = CreateTimer(12.0, Timer_FitnessTomEnd, client);
	g_clientEffectTimer[client][1] = CreateTimer(1.0, Timer_FitnessTom, client, TIMER_REPEAT);
	
	EmitSoundClient(SOUND_FITNESSTOM, client);
	
	return psyRTD_Continue;
}

public Action:Timer_FitnessTom(Handle:timer, any:client) {
	if (!g_bHasFitnessTom[client]) {
		return Plugin_Stop;
	} else {
		new Float:vec[3] = {0.0,0.0,1.0};
		ApplyForce(client,vec,350.0);
		
		return Plugin_Continue;
	}
}

public Action:Timer_FitnessTomEnd(Handle:timer, any:client) {
	g_bHasFitnessTom[client] = false;
	g_clientEffectTimer[client][0] = INVALID_HANDLE;
	g_clientEffectTimer[client][1] = INVALID_HANDLE;
		
	if (client && IsClientInGame(client) && !IsClientInKickQueue(client)) {
		psyRTD_EffectIsNowDisabled(client,g_iEffectNum[0]);
	}	
}



public psyRTDAction:EffectCantStandStill(client) {
	g_bHasCantStandStill[client] = true;
	
	g_clientEffectTimer[client][0] = CreateTimer(12.0, Timer_CantStandStillEnd, client);
	g_clientEffectTimer[client][1] = CreateTimer(0.8, Timer_CantStandStill, client, TIMER_REPEAT);
	
	EmitSoundClient(SOUND_CANTSTANDSTILL1, client);
	
	return psyRTD_Continue;
}

public Action:Timer_CantStandStill(Handle:timer, any:client) {
	if (!g_bHasCantStandStill[client]) {
		return Plugin_Stop;
	} else {
		new Float:vec[3];
		vec[0] = GetRandomFloat(-1.0,1.0);
		vec[1] = GetRandomFloat(-1.0,1.0);
		vec[2] = GetRandomFloat(-1.0,1.0);
		
		EmitSoundClient(SOUND_CANTSTANDSTILL2, client);
		ApplyForce(client,vec,600.0);
		
		return Plugin_Continue;
	}
}

public Action:Timer_CantStandStillEnd(Handle:timer, any:client) {
	g_bHasCantStandStill[client] = false;
	g_clientEffectTimer[client][0] = INVALID_HANDLE;
	g_clientEffectTimer[client][1] = INVALID_HANDLE;		
		
	if (client && IsClientInGame(client) && !IsClientInKickQueue(client)) {
		psyRTD_EffectIsNowDisabled(client,g_iEffectNum[1]);
	}	
}

public psyRTDAction:EffectPumpkin(client) {	
	g_clientEffectTimer[client][0] = CreateTimer(3.0, Timer_PumpkinEnd, client);
	//g_clientEffectTimer[client][1] = CreateTimer(0.8, Timer_CantStandStill, client, TIMER_REPEAT);
	
	EmitSoundClient(SOUND_PUMPKIN, client);
	new Float:vec[3];
	GetClientAbsOrigin(client, vec);
	SpawnPumpkin(vec);
	
	return psyRTD_Continue;
}


stock SpawnPumpkin(Float:pos[3]) {
	new iPumpkin = CreateEntityByName("tf_pumpkin_bomb");
	
	if(IsValidEntity(iPumpkin))
	{		
		SetEntProp(iPumpkin, Prop_Data, "m_CollisionGroup", 2);
		//SetEntProp(iPumpkin, Prop_Data, "m_usSolidFlags", 16);
	
		DispatchSpawn(iPumpkin);
		TeleportEntity(iPumpkin, pos, NULL_VECTOR, NULL_VECTOR);
	}
}

public Action:Timer_PumpkinEnd(Handle:timer, any:client) {
	g_clientEffectTimer[client][0] = INVALID_HANDLE;
		
	if (client && IsClientInGame(client) && !IsClientInKickQueue(client)) {
		psyRTD_EffectIsNowDisabled(client, g_iEffectNum[3]);
	}	
}